<template>
  <div>
    <TopFilter ref="filter" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partAccessAccessInventoryTabsAdd"
      :params="fetchParams"
      :fetchapi="findPartInfo"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="select"
      selectionType="single"
    >
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="saveDialog">{{ '保存' }}</el-button>
    </div>
  </div>
</template>
<script>
import { findPartInfo, addNewInventoryNo } from '@/api/part/access/accessInventory';
export default {
  data() {
    return {
      topFilterList: this.createFilList(),
      columns: this.createColumns(),
      fetchParams: {},
      fromData: {},
      tableData: {},
      findPartInfo: findPartInfo
    };
  },
  methods: {
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {},
    select(row) {
      this.tableData = row[0];
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async saveDialog() {
      let json = Object.assign({}, this.fetchParams, {
        dms_search: [this.tableData]
      });
      console.log(json);
      let res = await addNewInventoryNo(json);
      console.log(res);
      if (res == 0) {
        this.error();
        this.$emit('getDataAdd', false, 'error');
      } else {
        this.$emit('getDataAdd', false, this.tableData);
      }
    },
    error() {
      this.$notify.error({
        title: '错误',
        message: '填写错误，配件代码已存在'
      });
    },
    createFilList() {
      return [
        {
          type: 'INPUT',
          label: '仓库',
          fieldName: 'storageCode',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '配件类别',
          fieldName: 'inventoryNo9',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'inventoryNo8',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '车型组',
          fieldName: 'inventoryNo6',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'spellCode',
          readonly: false
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          readonly: false
        },
        {
          type: 'SELECT',
          label: '库存大于0',
          fieldName: 'inventoryNo2',
          readonly: false,
          itemList: [
            { value: '1', text: '是' },
            { value: '0', text: '否·' }
          ]
        },
        {
          type: 'SELECT',
          label: '是否停用',
          fieldName: 'inventoryNo1',
          readonly: false,
          itemList: [
            { value: '1', text: '是' },
            { value: '0', text: '否·' }
          ]
        }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型组集',
          dataIndex: 'storageName5',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售价',
          dataIndex: 'storageName6',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '成本价',
          dataIndex: 'costPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账面库存',
          dataIndex: 'storageNamew',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
<style></style>
